/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client;

import org.nuxeo.gwt.habyt.templates.client.widgets.PopupPanelEx;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class MessagePopup {

    protected PopupPanel popup;

    protected Timer timer = new Timer() {
        @Override
        public void run() {
            if (popup != null) {
                popup.hide();
            }
        }
    };

    public static interface HintImages extends ClientBundle {

        @Source("info.png")
        ImageResource info();

    }

    private static HintImages IMAGES;

    protected String message;

    public MessagePopup(String message) {
        this.message = message;
    }

    public static HintImages getImages() {
        if (IMAGES == null) {
            IMAGES = GWT.create(HintImages.class);
        }
        return IMAGES;
    }

    public String getMessage() {
        return message;
    }

    protected PopupPanel createPopup() {
        PopupPanel popup = new PopupPanelEx();
        popup.setStyleName("message-popup");
        popup.setAutoHideEnabled(true);
        popup.setAnimationEnabled(true);
        HorizontalPanel content = new HorizontalPanel();
        content.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        content.add(new Image(getImages().info()));
        content.add(new HTML(message));
        content.setWidth("350px");
        popup.setWidget(content);
        return popup;
    }

    public void show() {
        popup = createPopup();
        popup.center();
        timer.schedule(2000);
    }

    public void hide() {
        popup.hide();
        popup = null;
        timer.cancel();
    }

}
